package pit.com.ordermobilesystem;

import java.lang.Thread.UncaughtExceptionHandler;

import org.json.JSONException;
import org.json.JSONObject;

import com.nostra13.universalimageloader.core.ImageLoader;

import pit.com.ordermobilesystem.widget.CustomProgressDialog;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.ActionBarActivity;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;


/**
 * 
 * @author NMTHONG
 * @since 17/5/2013
 */
public class BaseFragmentActivity extends ActionBarActivity implements BaseFragmentInterface{
	CustomProgressDialog progressDialog;
	AlertDialog alertDialog;
	public ImageLoader imageLoader = ImageLoader.getInstance();
	boolean isNotifyLogin = false;
	Intent intentNotifyLogin;	
	
	
    @Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
//		Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() {
//			
//			@Override
//			public void uncaughtException(Thread thread, Throwable ex) {
//				ex.printStackTrace();
//				
//			}
//		});		
	}

	/**
     * @author NMTHONG
     * @since 17/5/2013
     * @param title 
     * @param message  
     * @param onCancel
     */
	public void showProgressDialog(DialogInterface.OnCancelListener onCancel){
		if (progressDialog == null) {
			progressDialog = new CustomProgressDialog(this);
			progressDialog.setOnCancelListener(onCancel);
		}
		progressDialog.show();
	
	}
	
	/**
	 * @author NMTHONG
	 * @since 17/5/2013
	 */
	public void hideProgressDialog(){
        if (progressDialog != null) {
        	progressDialog.dismiss();
        	progressDialog = null;
        }
	
	}	
	
	/**
	 * @author NMTHONG
	 * @since 17/5/2013
	 * @param title
	 * @param message
	 */
    public void showAlert(String title, String message) {
        new AlertDialog.Builder(this)
                .setTitle(title)
                .setMessage(message)
                .setPositiveButton(R.string.ok, null)
                .show();
    }   
    
	/**
	 * Hides the soft keyboard
	 */
	public void hideSoftKeyboard() {
		if (getCurrentFocus() != null) {
			InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
			inputMethodManager.hideSoftInputFromWindow(getCurrentFocus()
					.getWindowToken(), 0);
		}
	} 
    
	public void notifyLogin(JSONObject parameter) {

	}	
	
	BroadcastReceiver notifyLoginReceiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			isNotifyLogin = true;
			intentNotifyLogin = intent;
		}

	};

	@Override
	protected void onResume() {
		if (isNotifyLogin == true) {
			isNotifyLogin = false;
			if (intentNotifyLogin != null) {
				JSONObject data;
				try {
					data = new JSONObject(
							intentNotifyLogin.getStringExtra("data"));
					notifyLogin(data);
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			} else {
				notifyLogin(null);
			}
			intentNotifyLogin = null;
		}
		super.onResume();
	}
	
	@Override
	protected void onDestroy() {	
		hideProgressDialog();
		super.onDestroy();
	}    
}
